//
//  ViewController.m
//  test_simple_project
//
//  增强的ViewController，包含用户自定义方法和属性
//

#import "ViewController.h"
#import "CustomDataManager.h"
#import "CustomUtilities.h"

// 自定义私有接口 - 可混淆
@interface ViewController () <CustomDataManagerDelegate>

// 自定义私有属性 - 可混淆
@property (nonatomic, strong) CustomDataManager *customDataManager;
@property (nonatomic, strong) NSMutableArray *customUserActions;
@property (nonatomic, assign) NSInteger customSessionCounter;
@property (nonatomic, strong) NSTimer *customUpdateTimer;

// 自定义私有方法声明 - 可混淆
- (void)setupCustomComponents;
- (void)initializeCustomData;
- (void)startCustomTimer;
- (void)stopCustomTimer;
- (void)handleCustomTimerTick:(NSTimer *)timer;
- (void)performCustomDataProcessing;
- (void)logCustomUserAction:(NSString *)action;

@end

@implementation ViewController

#pragma mark - Lifecycle Methods (系统方法，不可混淆)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 调用自定义初始化方法 - 可混淆
    [self setupCustomComponents];
    [self initializeCustomData];
    [self startCustomTimer];
    
    NSLog(@"ViewController loaded with custom enhancements");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 自定义视图出现逻辑 - 可混淆
    [self logCustomUserAction:@"View Will Appear"];
    [self performCustomDataProcessing];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    // 自定义视图消失逻辑 - 可混淆
    [self logCustomUserAction:@"View Did Disappear"];
    [self stopCustomTimer];
}

- (void)dealloc {
    [self stopCustomTimer];
    NSLog(@"ViewController deallocated");
}

#pragma mark - Custom Setup Methods (可混淆)

- (void)setupCustomComponents {
    // 初始化自定义数据管理器
    self.customDataManager = [CustomDataManager sharedCustomManager];
    self.customDataManager.customDelegate = self;
    
    // 初始化自定义用户行为数组
    self.customUserActions = [[NSMutableArray alloc] init];
    
    // 初始化自定义会话计数器
    self.customSessionCounter = 0;
    
    NSLog(@"Custom components setup completed");
}

- (void)initializeCustomData {
    // 创建一些示例自定义数据
    CustomDataModel *customItem1 = [[CustomDataModel alloc] initWithIdentifier:@"CUSTOM_001" 
                                                                          title:@"First Custom Item"];
    customItem1.customPriority = 8;
    
    CustomDataModel *customItem2 = [[CustomDataModel alloc] initWithIdentifier:@"CUSTOM_002" 
                                                                          title:@"Second Custom Item"];
    customItem2.customPriority = 5;
    
    CustomDataModel *customItem3 = [[CustomDataModel alloc] initWithIdentifier:@"CUSTOM_003" 
                                                                          title:@"Third Custom Item"];
    customItem3.customPriority = 9;
    
    // 添加到自定义数据管理器
    [self.customDataManager addCustomDataItem:customItem1];
    [self.customDataManager addCustomDataItem:customItem2];
    [self.customDataManager addCustomDataItem:customItem3];
    
    NSLog(@"Custom data initialization completed with %ld items", 
          (long)self.customDataManager.customItemCount);
}

- (void)startCustomTimer {
    if (!self.customUpdateTimer) {
        self.customUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
                                                                   target:self
                                                                 selector:@selector(handleCustomTimerTick:)
                                                                 userInfo:nil
                                                                  repeats:YES];
        NSLog(@"Custom timer started");
    }
}

- (void)stopCustomTimer {
    if (self.customUpdateTimer) {
        [self.customUpdateTimer invalidate];
        self.customUpdateTimer = nil;
        NSLog(@"Custom timer stopped");
    }
}

- (void)handleCustomTimerTick:(NSTimer *)timer {
    self.customSessionCounter++;
    
    // 执行自定义定时任务
    [self performCustomDataProcessing];
    [self logCustomUserAction:[NSString stringWithFormat:@"Timer Tick #%ld", (long)self.customSessionCounter]];
    
    // 每10次tick执行一次数据清理
    if (self.customSessionCounter % 10 == 0) {
        [self performCustomDataCleanup];
    }
    
    NSLog(@"Custom timer tick #%ld processed", (long)self.customSessionCounter);
}

- (void)performCustomDataProcessing {
    // 获取高优先级的自定义项目
    NSArray *customHighPriorityItems = [self.customDataManager filterCustomItemsByPriority:7];
    
    // 处理每个高优先级项目
    for (CustomDataModel *customItem in customHighPriorityItems) {
        NSString *customProcessedDescription = [customItem customFormattedDescription];
        NSString *customHashedDescription = [CustomUtilities customHashString:customProcessedDescription];
        
        NSLog(@"Processed custom item: %@ -> Hash: %@", 
              customItem.customIdentifier, 
              [customHashedDescription substringToIndex:MIN(8, customHashedDescription.length)]);
    }
    
    // 使用自定义工具进行数据验证
    NSString *customTestEmail = @"test@example.com";
    BOOL customEmailValid = [CustomUtilities customValidateEmail:customTestEmail];
    
    if (customEmailValid) {
        [self logCustomUserAction:@"Email validation passed"];
    }
}

- (void)performCustomDataCleanup {
    // 自定义数据清理逻辑
    NSInteger customItemCountBefore = self.customDataManager.customItemCount;
    
    // 移除低优先级项目
    NSArray *customLowPriorityItems = [self.customDataManager filterCustomItemsByPriority:0];
    for (CustomDataModel *customItem in customLowPriorityItems) {
        if (customItem.customPriority < 3) {
            [self.customDataManager removeCustomDataItem:customItem];
        }
    }
    
    NSInteger customItemCountAfter = self.customDataManager.customItemCount;
    NSInteger customRemovedCount = customItemCountBefore - customItemCountAfter;
    
    if (customRemovedCount > 0) {
        NSLog(@"Custom cleanup removed %ld low priority items", (long)customRemovedCount);
        [self logCustomUserAction:[NSString stringWithFormat:@"Cleanup removed %ld items", (long)customRemovedCount]];
    }
}

- (void)logCustomUserAction:(NSString *)action {
    if (action && action.length > 0) {
        NSString *customTimestamp = [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                                   dateStyle:NSDateFormatterNoStyle
                                                                   timeStyle:NSDateFormatterMediumStyle];
        
        NSDictionary *customActionDict = @{
            @"action": action,
            @"timestamp": customTimestamp,
            @"session_counter": @(self.customSessionCounter)
        };
        
        [self.customUserActions addObject:customActionDict];
        
        // 限制用户行为日志数量
        if (self.customUserActions.count > 100) {
            [self.customUserActions removeObjectsInRange:NSMakeRange(0, 50)];
        }
        
        NSLog(@"Logged custom user action: %@", action);
    }
}

#pragma mark - Custom Public Methods (可混淆)

- (void)performCustomBatchOperation {
    NSLog(@"Starting custom batch operation...");
    
    // 创建批量自定义数据
    NSMutableArray *customBatchItems = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 1; i <= 5; i++) {
        NSString *customIdentifier = [NSString stringWithFormat:@"BATCH_%03ld", (long)i];
        NSString *customTitle = [NSString stringWithFormat:@"Batch Item #%ld", (long)i];
        
        CustomDataModel *customBatchItem = [[CustomDataModel alloc] initWithIdentifier:customIdentifier 
                                                                                  title:customTitle];
        customBatchItem.customPriority = [CustomUtilities customGenerateRandomNumber:1 max:10];
        
        [customBatchItems addObject:customBatchItem];
        [self.customDataManager addCustomDataItem:customBatchItem];
    }
    
    [self logCustomUserAction:[NSString stringWithFormat:@"Batch operation created %lu items", 
                              (unsigned long)customBatchItems.count]];
    
    NSLog(@"Custom batch operation completed with %lu items", (unsigned long)customBatchItems.count);
}

- (NSString *)generateCustomReport {
    NSMutableString *customReport = [[NSMutableString alloc] init];
    
    [customReport appendString:@"=== Custom Data Report ===\n"];
    [customReport appendFormat:@"Session Counter: %ld\n", (long)self.customSessionCounter];
    [customReport appendFormat:@"Total Custom Items: %ld\n", (long)self.customDataManager.customItemCount];
    [customReport appendFormat:@"User Actions Logged: %lu\n", (unsigned long)self.customUserActions.count];
    
    // 添加高优先级项目信息
    NSArray *customHighPriorityItems = [self.customDataManager filterCustomItemsByPriority:8];
    [customReport appendFormat:@"High Priority Items: %lu\n", (unsigned long)customHighPriorityItems.count];
    
    for (CustomDataModel *customItem in customHighPriorityItems) {
        [customReport appendFormat:@"  - %@: %@\n", 
         customItem.customIdentifier, 
         customItem.customTitle];
    }
    
    [customReport appendString:@"========================\n"];
    
    return [customReport copy];
}

#pragma mark - CustomDataManagerDelegate (可混淆)

- (void)dataManager:(id)manager didUpdateData:(NSArray *)newData {
    NSLog(@"Custom data manager updated with %lu items", (unsigned long)newData.count);
    [self logCustomUserAction:[NSString stringWithFormat:@"Data updated: %lu items", (unsigned long)newData.count]];
}

- (void)dataManager:(id)manager didFailWithError:(NSError *)error {
    NSLog(@"Custom data manager failed with error: %@", error.localizedDescription);
    [self logCustomUserAction:[NSString stringWithFormat:@"Data error: %@", error.localizedDescription]];
}

@end
